package com.chenu.gamscriptman.controller;

import com.chenu.gamscriptman.compoent.Global;
import com.chenu.gamscriptman.domain.Game;
import com.chenu.gamscriptman.domain.GameData;
import com.chenu.gamscriptman.domain.noindb.PageBean;
import com.chenu.gamscriptman.domain.noindb.Statistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author flyan
 * date 2019-12-29
 * @function 游戏控制器，它用于处理游戏任务完成需要视图的服务后返回的回复消息
 */
@Controller
@ApiIgnore
public class GameController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate jsonRedisTemplate;
    
    /**
     * 游戏管理/列出所有用户
     */
    @GetMapping("/games")
    public String games(
            Model model
    ){
        // 放入分页数据和查询参数
        PageBean<Game> game_page = (PageBean<Game>) jsonRedisTemplate.opsForValue().get(Global.REDIS_GAME_PAGES);
        model.addAttribute(Global.REDIS_GAME_PAGES, game_page);
        Game search_game = (Game) jsonRedisTemplate.opsForValue().get(Global.REDIS_SEARCH_GAME);
        model.addAttribute(Global.REDIS_SEARCH_GAME, search_game);
        /* 放入统计信息 */
        Statistics game_statistics = (Statistics) jsonRedisTemplate.opsForValue().get(Global.REDIS_GAME_STATISTICS);
        model.addAttribute(Global.REDIS_GAME_STATISTICS, game_statistics);
        /* 所有的游戏名称 */
        model.addAttribute(Global.REDIS_SEARCH_GAME_NAME_LIST,
                (List<String>)jsonRedisTemplate.opsForValue().get(Global.REDIS_SEARCH_GAME_NAME_LIST));

        return "game/list";
    }

    /**
     * 前往添加/编辑页面
     * @param model
     * @return
     */
    @GetMapping("/game")
    public String gameAddOrEdit(
            Model model
    ){
        // 放入编辑的用户
        Game editGame = (Game) jsonRedisTemplate.opsForValue().get(Global.REDIS_EDIT_GAME);
        model.addAttribute(Global.REDIS_EDIT_GAME, editGame);

        return "game/addOrEdit";
    }

}
